

import numpy as np
from pylab import *

def loadSimpleData():
    dataMat=np.matrix([[1.0,2.1],
                       [2.0,1.1],
                       [1.3,1.0],
                       [1.0,1.0],
                       [2.0,1.0]])
    labels=[1.0,1.0,-1.0,-1.0,1.0]
    labelMat=np.mat(labels)
    print("load data")
    print(shape(labels))
    print(shape(labelMat))
    return dataMat, labels

def splitClass(dataMat, labels):
    x1=[]
    x2=[]
    y1=[]
    y2=[]
    x=dataMat[:,0].getA1()
    y=dataMat[:,1].getA1()
    print(labels)
    for i in range(0,len(labels)):
        l=labels[i]
        if abs(l-1.0)<0.001:
            x1.append(x[i])
            y1.append(y[i])
        else:
            x2.append(x[i])
            y2.append(y[i])

    return x1,y1,x2,y2

def stumpClassify(dataMat,dimen,thresVal,thresIneq):
    retArr=np.ones((shape(dataMat)[0],1))
    print("stump classify")
    print(dataMat[:,dimen])
    print(dataMat[:,dimen]>thresVal)
    if(thresVal=="lt"):
        retArr[dataMat[:,dimen]>thresVal]=-1.0
    else:
        retArr[dataMat[:,dimen]<=thresVal]=-1.0

    return retArr

def buildClassify(dataMat, labels, D):
    print(shape(dataMat))
    m,n = np.shape(dataMat)
    minErr=inf
    for dim in range(n):
        dimMax=max(dataMat[:,dim])
        dimMin=min(dataMat[:,dim])
        numSteps=10.0
        stepSize=(dimMax-dimMin)/numSteps
        for step in range(-1, int(numSteps)+1):
            thresVal=dimMin+step*stepSize
            for thresIneq in ['lt', 'gt']:
                retArr = stumpClassify(dataMat,dim,thresVal,thresIneq)
                labelMat=np.mat(labels).T
                print(np.shape(labelMat))
                errArr=np.ones((len(labels),1))
                print(shape(errArr))
                print(shape(retArr))
                print(shape(labelMat))
                errArr[retArr==labelMat]=0.0
                err=D.T*errArr
                print(err)
                if(err<minErr):
                    minErr=err

    return minErr



def plotSimpleData(dataMat, labels):
    print(dataMat)
    print(dataMat.size)
    arr = dataMat.getA1()
    print(arr)
    x=dataMat[:,0].getA1()
    y=dataMat[:,1].getA1()
    #for i in range(0,5):
    #    x.append(dataMat[i,0])
    #    y.append(dataMat[i,1])
    
    print(x)
    print(y)
    x1,y1,x2,y2 = splitClass(dataMat,labels)
    print(x1)
    print(y1)
    print(x2)
    print(y2)
    scatter(x1,y1,50, color ='red')
    scatter(x2,y2,50, color ='blue')
    show()